using micromsg;
using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Protocol;
using MicroMsg.Scene;
using MicroMsg.Storage;
using System;
namespace MicroMsg.Plugin.FaceBook.Scene
{
	public class NetSceneFaceBookAuth : NetSceneBaseEx<FaceBookAuthRequest, FaceBookAuthResponse, FaceBookAuthRequest.Builder>
	{
		private const string TAG = "NetSceneFaceBookAuth";
		public const int MM_FACEBOOK_UNBIND = 0;
		public const int MM_FACEBOOK_BIND = 1;
		public const int MM_FACEBOOK_UPDATE_TOKEN = 2;
		private string mAccessToken = "";
		public bool doScene(int opType, string AccessToken)
		{
			if (string.IsNullOrEmpty(AccessToken))
			{
				return false;
			}
			if (opType == 2)
			{
				Account curAccount = AccountMgr.getCurAccount();
				curAccount.FBToken = AccessToken;
				AccountMgr.updateAccount();
			}
			this.mAccessToken = AccessToken;
			base.beginBuilder();
			this.mBuilder.BaseRequest = NetSceneBase.makeBaseRequest(0);
			this.mBuilder.OpType = (uint)opType;
			this.mBuilder.AccessToken = AccessToken;
			this.mSessionPack.mCmdUri = "/cgi-bin/micromsg-bin/facebookauth";
			this.mSessionPack.mConnectMode = 2;
			base.endBuilder();
			return true;
		}
		protected override void onFailed(FaceBookAuthRequest request, FaceBookAuthResponse response)
		{
			Log.smethod_1("NetSceneFaceBookAuth", "FaceBook Auth failed system error!");
			EventCenter.postEvent(EventConst.ON_NETSCENE_FACEBOOK_EVENT, new FBAuthRet(false, RetConst.MM_ERR_CLIENT, request.OpType), null);
		}
		protected override void onSuccess(FaceBookAuthRequest request, FaceBookAuthResponse response)
		{
			if (response == null || response.BaseResponse.Ret != 0)
			{
				Log.smethod_1("NetSceneFaceBookAuth", "FaceBook Auth response not invalidate response.BaseResponse.Ret = " + ((RetConst)response.BaseResponse.Ret).ToString());
				EventCenter.postEvent(EventConst.ON_NETSCENE_FACEBOOK_EVENT, new FBAuthRet(false, (RetConst)response.BaseResponse.Ret, request.OpType), null);
				return;
			}
			Log.smethod_4("NetSceneFaceBookAuth", string.Concat(new object[]
			{
				"Facebook Auth success! FBUserID =",
				response.FBUserID,
				"FBUserName = ",
				response.FBUserName
			}));
			ulong fBUserID = response.FBUserID;
			string fBUserName = response.FBUserName;
			EventCenter.postEvent(EventConst.ON_NETSCENE_FACEBOOK_EVENT, new FBAuthRet(true, (RetConst)response.BaseResponse.Ret, request.OpType), null);
			if (request.OpType == 0u)
			{
				Account curAccount = AccountMgr.getCurAccount();
				curAccount.FaceBookFlag = 0u;
				curAccount.FBToken = "";
				curAccount.FBUserID = 0uL;
				curAccount.FBUserName = "";
				AccountMgr.updateAccount();
				StorageMgr.fbContact.clearAll();
				ConfigMgr.setDouble("lastGetFBContactime", 0.0);
				return;
			}
			if (fBUserID > 0uL)
			{
				Account curAccount2 = AccountMgr.getCurAccount();
				curAccount2.FaceBookFlag = 1u;
				curAccount2.FBToken = this.mAccessToken;
				curAccount2.FBUserID = fBUserID;
				curAccount2.FBUserName = fBUserName;
				AccountMgr.updateAccount();
			}
		}
	}
}
